package com.lyw.game;

import com.lyw.constants.IniConstant;
import com.lyw.domain.GlobalData;
import com.lyw.helper.*;
import com.lyw.helper.Process;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@Component
public class Initialize extends Base {

    static boolean firstEnterMap; // 首次进图

    static int completedNum;      // 完成次数

    @Resource
    public MapData mapData;

    @Resource
    public GameCall gamecall;

    @Resource
    private Traverse traverse;

    @Resource
    public GameMap gameMap;

    @Resource
    private SendPack sendPack;

    @Resource
    private IniUtils iniUtils;

    @Resource
    private Task task;

    public void Init() {
        String modelName = "dnf.exe";
        int processId = Process.getProcessId(modelName);
        if (processId == 0) {
            logger.info("等待游戏运行...");
            do {
                processId = Process.getProcessId(modelName);
            } while (processId == 0);
        }

        // 初始化配置文件
        initConfigIni();

        // 设置全局进程id
        memory.setProcessId(processId);
        logger.info("加载成功-欢迎使用");
        logger.info("当前时间：{}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        initEmptyAddress();

        System.out.println(mapData.getRoleLevel());
//        System.out.println(mapData.getFame());
        System.out.println(mapData.getPl());
        System.out.println(mapData.getStat());
        traverse.packPickup();
        /*// 城镇处理
        if (mapData.getStat() == 1*//* && mapData.isTown()*//*) {
            townHandle();
        }

        // 在地图内
        if (mapData.getStat() == 3) {
//            if (!firstEnterMap && !mapData.isTown()) {
            if (!firstEnterMap) {
                //  透明call
                gamecall.hideCall(gamecall.personPtr());
                //  sss评分
                Random random = new Random();
                int nextInt = random.nextInt(9999999 - 5201314 + 1) + 5201314;
                memory.writeLong(memory.readLong(Address.PFAddr) + Address.CEPfAddr, nextInt);
                firstEnterMap = true;
            }

            // 跟随怪物
            if (iniUtils.read("自动配置", "跟随打怪", Integer.class) > 0) {
                logger.debug("开始跟随怪物");
                traverse.FollowMonster();
            }

            // 过图
            if (mapData.isOpenDoor() && !mapData.isBossRoom()) {
                // 捡物品
                traverse.packPickup();
                // 过图
//                passMap();
            }

            *//*if (mapData.isBossRoom() && mapData.isPass()) {
                // 捡物品
                traverse.packPickup();
                // 刷图计次
                passBoss();
                // 退出副本
                quitMap();
                firstEnterMap = false;
            }*//*
        }*/

        /*String modelName = "dnf.exe";
        int processId = Process.getProcessId(modelName);
        if (processId == 0) {
            logger.info("等待游戏运行...");
            do {
                processId = Process.getProcessId(modelName);
            } while (processId == 0);
        }

        // 设置全局进程id
        memory.setProcessId(processId);*/

    }

    /**
     * 城镇处理
     */
    private void townHandle() {

        Timer.sleep(500);

        // 瞬移至地图
        String numbers = iniUtils.read("自动配置", "普通地图", String.class);
        int[] mapIds = Strings.splitToIntArray(numbers, ",");
        Integer mapLevel = iniUtils.read("自动配置", "地图难度", Integer.class);
        GlobalData.mapId = mapIds[1];
        GlobalData.mapLevel = mapLevel;

        // 选择地图
        Timer.sleep(500);
        gamecall.areaCall(GlobalData.mapId);
        Timer.sleep(500);
        selectMap();

        // 进入地图
        enterMap(GlobalData.mapId, GlobalData.mapLevel);
    }

    /**
     * 进入地图
     */
    private void enterMap(int mapId, int mapLevel) {
        if (mapLevel == 5) {
            for (int i = 4; i >= 0; i--) {
                if (mapData.getStat() == 3) {
                    break;
                }
                if (mapData.getStat() == 2) {
                    gamecall.goMapCall(mapId, i);
                    Timer.sleep(1000);
                }
                if (mapData.getStat() == 1) {
                    selectMap();
                }
            }
        } else {
            gamecall.goMapCall(mapId, mapLevel);
        }

        while (GlobalData.autoSwitch) {
            logger.debug("进入副本循环");
            Timer.sleep(200);
            if (mapData.getStat() == 3) {
                break;
            }
        }
    }

    /**
     * 选择地图
     */
    private void selectMap() {
//        while (GlobalData.autoSwitch) {
        while (true) {
            logger.debug("选图循环");
            Timer.sleep(200);
            sendPack.selectMap();
            if (mapData.getStat() == 2 || mapData.getStat() == 3) {
                break;
            }
        }
    }

    /**
     * 初始化空白地址
     */
    private void initEmptyAddress() {
        Address.RwKbAddr = memory.allocate(4096);
        Address.NcBhKbAddr = memory.allocate(4096);
        Address.JnKbAddr = memory.allocate(4096);
        Address.GtKbAddr = memory.allocate(4096);

        logger.info("人物基址 {}", Long.toHexString(Address.RwKbAddr));
        logger.info("内存汇编 {}", Long.toHexString(Address.NcBhKbAddr));
        logger.info("技能空白 {}", Long.toHexString(Address.JnKbAddr));
        logger.info("过图空白 {}", Long.toHexString(Address.GtKbAddr));
    }

    private void initConfigIni() {
        // 判断是否存在全局计次配置文件
        FileUtils fileUtils = new FileUtils(IniConstant.Config);
        if (!fileUtils.exists()) {
            fileUtils.create();
            IniUtils iniUtils = new IniUtils();
            iniUtils.setFilename(IniConstant.Config).write("default", "count", 0);
        }

        FileUtils file = new FileUtils(IniConstant.Helper);
        if (!file.exists()) {
            return;
        }
    }
}